package com.zx.BookSeaReader.ui.booklistcreate;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.zx.BookSeaReader.base.BasePresenter;
import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.creator.DialogCreator;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookListBean;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.BookListService;
import com.zx.BookSeaReader.greendao.service.BookService;
import com.zx.BookSeaReader.greendao.service.SearchHistoryService;
import com.zx.BookSeaReader.ui.search.SearchBookActivity;
import com.zx.BookSeaReader.util.TextHelper;
import com.zx.BookSeaReader.widget.itemdecoration.DividerItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * @Author: ZX
 * @Time: 2020/4/20
 * @Description: 功能描述
 */
public class BookListCreatePresenter implements BasePresenter {

    private BookListCreateActivity mBookListCreateActivity;
    private BookService mBookService;
    private BookListService mBookListService;
    private UserBean userBean;
    private String title;
    private String content;
    private List<Book> selectedBooks = new ArrayList<>();
    private List<Book> collBooks = new ArrayList<>();
    private int BooksCount=0;
    private CreateBookListAdapter mCreateBookListAdapter;

    private Handler mHandle = new Handler(message -> {
        switch (message.what){
            case 1:
                mBookListService.addBookList(selectedBooks,userBean,title,content,true);
                break;
            case 2:
                mBookListService.addBookList(selectedBooks,userBean,title,content,false);
                break;


        }
        return false;
    });


    public BookListCreatePresenter(BookListCreateActivity bookListCreateActivity) {
        mBookListCreateActivity = bookListCreateActivity;
        mCreateBookListAdapter = new CreateBookListAdapter();
        mBookService=new BookService();
        mBookListService=new BookListService();
    }


    @Override
    public void start() {
        userBean = (UserBean)mBookListCreateActivity.getIntent().getSerializableExtra(APPCONST.USER);
        collBooks=mBookService.getSubBook(userBean.getUsername());

        mBookListCreateActivity.getRvCreateBooks().setLayoutManager(new LinearLayoutManager(mBookListCreateActivity));
        mBookListCreateActivity.getRvCreateBooks().addItemDecoration(new DividerItemDecoration(mBookListCreateActivity));
        mBookListCreateActivity.getRvCreateBooks().setAdapter(mCreateBookListAdapter);
        init();

    }
    private void init(){
        mCreateBookListAdapter.refreshItems(collBooks);

        mCreateBookListAdapter.setOnItemClickListener(
                (v, pos) -> {
                    boolean check =  collBooks.get(pos).getIsLocal();
                    if(check){
                        collBooks.get(pos).setIsLocal(false);
                        selectedBooks.remove(collBooks.get(pos));
                        BooksCount--;
                        if(BooksCount!=collBooks.size()){
                            mBookListCreateActivity.getCbAllBooks().setChecked(false);
                        }
                    }else {
                        selectedBooks.add(collBooks.get(pos));
                        collBooks.get(pos).setIsLocal(true);
                        BooksCount++;
                        if(BooksCount==collBooks.size()){
                            mBookListCreateActivity.getCbAllBooks().setChecked(true);
                        }
                    }
                    mCreateBookListAdapter.refreshItems(collBooks);

        });

        mBookListCreateActivity.getCbAllBooks().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if(isChecked){

                    if(selectedBooks.size()!=collBooks.size()){
                        selectedBooks.clear();
                        selectedBooks.addAll(collBooks);
                        TextHelper.showText(selectedBooks.size()+" ");
                        for(Book book:collBooks){
                            book.setIsLocal(true);
                        }
                        mCreateBookListAdapter.refreshItems(collBooks);
                    }
                }else{
                    if(selectedBooks.size()==collBooks.size()){
                        for(Book book:collBooks){
                            book.setIsLocal(false);
                        }
                        selectedBooks.clear();
                        mCreateBookListAdapter.refreshItems(collBooks);
                    }
                }
            }
        });

        mBookListCreateActivity.getBtCreateBooks().setOnClickListener((view) -> {
            title = mBookListCreateActivity.getEtListTitle().getText().toString().trim();
            if(!TextUtils.isEmpty(title)){
                if(selectedBooks.size()!=0) {
                    boolean check = mBookListService.existBookList(userBean, title);
                    if (check) {
                        TextHelper.showText("你已经创建过同名的书单贴！");

                    } else {
                        addDescribe();
                    }
                }else {
                    TextHelper.showText("请选择要加入书单的书！");
                }

            }else{
                TextHelper.showText("请输入书单贴标题");
            }

        });
        mBookListCreateActivity.getllTitleBack().setOnClickListener(
                (v) ->{
                    mBookListCreateActivity.finish();
                }
        );
    }
    /**
     * 添加描述
     * @param
     */
    private void addDescribe(){
        final EditText editText = new EditText(mBookListCreateActivity);
        AlertDialog.Builder editDialog = new AlertDialog.Builder(mBookListCreateActivity);
        //editDialog.setIcon(R.mipmap.ic_launcher_round);
        editDialog.setTitle("请输入书单简介").setView(editText);
        editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                content = editText.getText().toString();
                if(!TextUtils.isEmpty(content)){
                    showDialog();
                    //TextHelper.showText("成功");
                }else {
                    TextHelper.showText("请输入书单描述");
                }

            }
        });
        editDialog.show();
    }

    private void showDialog(){
        DialogCreator.createCommonDialog(mBookListCreateActivity, "新建书单贴", "是否将此书单推荐给其他人？",
                true, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        mHandle.sendMessage(mHandle.obtainMessage(1));
                        TextHelper.showText("创建书单成功！已发布！");
                        dialogInterface.dismiss();

                    }
                }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mHandle.sendMessage(mHandle.obtainMessage(2));
                        mBookListService.addBookList(selectedBooks,userBean,title,content,false);
                        dialogInterface.dismiss();
                        TextHelper.showText("创建书单成功！未发布！");
                    }
                });
    }
}
